$(function () {
    var ana_datagrid = $("#ana_datagrid");
    var ana_dialog = $("#ana_dialog");
    var ana_form = $("#ana_form");


    ana_datagrid.datagrid({
        url: '/expenditureitems/getAnalysis.do',
        fit: true,
        fitColumns: true,
        toolbar: '#tb',
        columns: [
            [
                {field: "sort", title: "支出分类", width: 80},
                {field: "money", title: "支出金额", width: 80},
                {field: "time", title: "支出时间", width: 80},
                {field: "employee", title: "支出人员", width: 80},
                {field: "remark", title: "备注", width: 80}
            ]
        ],
        pagination: true,
        rownumbers: true,
        singleSelect: true,
        onLoadSuccess: function (data) {
            var xData = new Array();
            var yData = new Array();
            var datas = data.rows;
            for (var i in datas) {
                xData[i] = datas[i].money;
                yData[i] = datas[i].time;
            }

            // 基于准备好的dom，初始化echarts图表
            var myChart = echarts.init(document.getElementById('main'));
            var option = {
                tooltip: {
                    show: true
                },
                toolbox: {
                    show: true,
                    feature: {
                        mark: {show: true},
                        dataView: {show: true, readOnly: false},
                        magicType: {show: true, type: ['line', 'bar']},
                        restore: {show: true},
                        saveAsImage: {show: true}
                    }
                },
                legend: {
                    data: ['支出金额'],
                    x: 'left'
                },
                xAxis: [
                    {
                        type: 'category',
                        data: xData,
                    }
                ],
                yAxis: [
                    {
                        type: 'value'
                    }
                ],
                series: [
                    {
                        "name": "支出金额",
                        "type": "bar",
                        "data": yData,
                        markPoint: {
                            data: [
                                {type: 'max', name: '最大值'},
                                {type: 'min', name: '最小值'}
                            ]
                        },
                        markLine: {
                            data: [
                                {type: 'average', name: '平均值'}
                            ]
                        }
                    }
                ]
            };

            // 为echarts对象加载数据
            myChart.setOption(option);
        }
    })

    var methodObj = {
        exportXls: function () {
            window.location.href = "/saleAnalysis/exportXls.do";
        }
    }

    $("a[data-cmd]").click(function () {
        var methodName = $(this).data("cmd");
        methodObj[methodName]();
    })

})

